﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using CS424_9pts.Control;
using System.Configuration;

namespace CS424_9pts.Administration
{
    public partial class MaintenanceStaff_ViewEditDateStatus : System.Web.UI.Page
    {
        private string m_UpdateCommand;

        public string UpdateCommand
        {
            get { return m_UpdateCommand; }
            set { m_UpdateCommand = value; }
        }

        DataView dv;
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.DataBind();
            dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            dt = dv.ToTable();
        }

        protected void OnSelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent;

            var userName = dt.Rows[GridView1.SelectedIndex]["UserName"].ToString();
            var eMail = dt.Rows[GridView1.SelectedIndex]["Email"].ToString();
            string message;
            

            DropDownList d = (DropDownList)(gr.FindControl("DropDownList1"));
            int stt = d.SelectedIndex;
            string status = d.SelectedItem.Text;
            int Mid = int.Parse(((gr.FindControl("Mid")) as Label).Text);
            switch (stt)
            {
                case 1:
                    m_UpdateCommand = "UPDATE [Maintenance] SET [DateToCompany] = GETDATE(), [Status]= N'" + status + "'  WHERE [MaintenanceID] = " + Mid;
                    message = "Today, you send to us your car";
                    MailMan.SendMail(eMail, userName, message);  //Send mail                             
                    //MailMan.SendMail("dinhngoclan88@gmail.com", "cocon", message);  //Send mail                             
                    break;
                case 2:
                    m_UpdateCommand = "UPDATE [Maintenance] SET [DateToManufacturer] = GETDATE(), [Status]= N'" + status + "'    WHERE [MaintenanceID] = " + Mid;
          
                    break;
                case 3:
                    m_UpdateCommand = "UPDATE [Maintenance] SET [DateBackToCompany] = GETDATE(), [Status]= N'" + status + "'    WHERE [MaintenanceID] = " + Mid;
                    message = "Today, your car is returned to company. Please get your car home";
                    MailMan.SendMail(eMail, userName, message);  //Send mail
                    break;
                case 4:
                    m_UpdateCommand = "UPDATE [Maintenance] SET [DateBackToCustomer] = GETDATE(), [Status]= N'" + status + "'    WHERE [MaintenanceID] = " + Mid;
                    break;
            }
            SqlDataSource1.UpdateCommand = m_UpdateCommand;
            //SqlDataSource1.Update();
        }


    }
}